﻿using DeviceDetectorNET.Cache;
using Ebusiness_ProductService.Services;
using Ebusiness_ProductService.Services.Dtos.Product;
using Ebusiness_UserService.Redis;
using Ebusiness_UserService.Services;
using Ebusiness_UserService.Services.Dtos.User;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Caching;

namespace Ebusiness_WebSites.Controllers;

/// <summary>
///首页控制器 给前端页面展示数据
/// </summary>
[Route("User/[action]")]
[ApiController]
public class UserController : AbpController
{
    public IUserService userService { get; set; }

    //public IDistributedCache<BookCacheItem> _cache { get; set; }

    public IDistributedCache<List<UserDto>> _listCache { get; set; }

    public ILogger<ProductController> logger { get; set; }  

    public IConfiguration configuration { get; set; }




    /// <summary>
    /// 查询商品数据
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public string Index()
    {
        return "xxxx";
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<PagedResultDto<UserDto>> GetUserList()
    {
        #region  服务降级使用nacos中心配置
        {
            //服务降级：改动服务接口功能。
            //​ 例如：查询MySQL中商品数据，换成查询redis中的商品数据。
            //​ 例如：查询redis中的商品数据。换成查询MySQL中商品数据
            //​ 服务降级应用场景：大促期间，如果并发大了。换成查询redis。
            //​ 大促已过，如果并发小了。换成查询mysql。
            //​ 核心工具：配置中心
            //​ 前提：电商网站中商品数据存储到redis中。
            //​ 环境：redis准备
            //​ 技术：Volo.Abp.Caching.StackExchangeRedis
            //​ 具体技术：IDistributedCache<List>
            //var w = configuration["nihaoa"];
            //var sww = configuration["name"];
            //PagedResultDto<UserDto> ww = new PagedResultDto<UserDto>();

            //if (w == "1")  //数据库中获取
            //{
            //    var t = await userService.GetListAsync(new PagedAndSortedResultRequestDto());
            //    var tt = t.Items.ToList();
            //    //存储到redis
            //    await _listCache.SetAsync("UserDto:list", tt, new DistributedCacheEntryOptions
            //    {
            //        AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30)
            //    });
            //    ww.Items = tt;
            //}
            //else
            //{  //从redis缓存中获取
            //    ww.Items = await _listCache.GetAsync("UserDto:list");
            //}
            //return ww;

            var t = await userService.GetListAsync(new PagedAndSortedResultRequestDto());
            logger.LogInformation("网站查询商品服务");
            return t;
        }
        #endregion

    }

    [HttpPost]
    public async Task<string> LoginByPhoneAndPwd(LoginByPhoneAndPwdRequest req)
    {
        return await userService.LoginByPhoneAndPwd(req);
    }
}
